home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / ISCAN2.C < prev    next >
C/C++ Source or Header  |  1993-05-20  |  12KB  |  416 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* iscan2.c */
  20. /* Level 2 extensions for token scanner */
  21. #include "math_.h"
  22. #include "ghost.h"
  23. #include "stream.h"
  24. #include "errors.h"
  25. #include "alloc.h"
  26. #include "dict.h"
  27. #include "dstack.h"            /* for immediately evaluated names */
  28. #include "iname.h"
  29. #include "iscan.h"            /* for scan_BOS */
  30. #include "iutil.h"
  31. #include "ivmspace.h"
  32. #include "ostack.h"
  33. #include "save.h"            /* for alloc_refs */
  34. #include "store.h"
  35. #include "btoken.h"
  36. #include "bseq.h"
  37. #include "bnum.h"
  38.  
  39. /* Import the system and user name tables */
  40. extern ref system_names, user_names;
  41.  
  42. /* Import the ASCII85 decoding stream procedures */
  43. extern const stream_procs s_A85D_procs;
  44.  
  45. /* Forward references */
  46. private    int    scan_binary_sequence(P2(stream *, ref *));
  47.  
  48. /* Set up to scan an ASCII85 string literal.  Called from the main scanner */
  49. /* when it encounters the sequence <~. */
  50. void
  51. scan_ascii85_setup(stream *s, stream *sstrm, byte *buf, uint bsize)
  52. {    s_std_init(s, buf, bsize, &s_A85D_procs, s_mode_read);
  53.     s->end_status = 0;
  54.     s->file = 0;            /* not a file stream */
  55.     s->strm = sstrm;
  56.     s->strm_is_temp = 0;
  57. }
  58.  
  59. /* Scan a binary token.  Called from the main scanner */
  60. /* when it encounters an ASCII code 128-159, */
  61. /* if binary tokens are being recognized (object format != 0). */
  62. int
  63. scan_binary_token(register stream *s, ref *pref, int tcode)
  64. {    int num_format, code;
  65.     uint arg;
  66.     ushort ashort;
  67.     long nidx;
  68.     if ( seofp(s) )
  69.         return_error(e_syntaxerror);
  70.     switch ( tcode )
  71.        {
  72.     case bt_seq_IEEE_msb:
  73.         s->num_format = num_msb + num_float_IEEE; goto bseq;
  74.     case bt_seq_IEEE_lsb:
  75.         s->num_format = num_lsb + num_float_IEEE; goto bseq;
  76.     case bt_seq_native_msb:
  77.         s->num_format = num_msb + num_float_native; goto bseq;
  78.     case bt_seq_native_lsb:
  79.         s->num_format = num_lsb + num_float_native;
  80. bseq:        return scan_binary_sequence(s, pref);
  81.     case bt_int32_msb:
  82.         num_format = num_msb + num_int32; goto num;
  83.     case bt_int32_lsb:
  84.         num_format = num_lsb + num_int32; goto num;
  85.     case bt_int16_msb:
  86.         num_format = num_msb + num_int16; goto num;
  87.     case bt_int16_lsb:
  88.         num_format = num_lsb + num_int16; goto num;
  89.     case bt_int8:
  90.         make_int(pref, (sgetc(s) ^ 128) - 128);
  91.         return 0;
  92.     case bt_fixed:
  93.         num_format = sgetc(s);
  94.         if ( !num_is_valid(num_format) )
  95.             return_error(e_syntaxerror);
  96.         goto num;
  97.     case bt_float_IEEE_msb:
  98.         num_format = num_msb + num_float_IEEE; goto num;
  99.     case bt_float_IEEE_lsb:
  100.         num_format = num_lsb + num_float_IEEE; goto num;
  101.     case bt_float_native:
  102.         num_format = num_float_native;
  103. num:        s->num_format = num_format;
  104.         code = sget_encoded_number(s, pref);
  105.         switch ( code )
  106.            {
  107.         case t_integer: case t_real:
  108.             r_set_type(pref, code);
  109.             break;
  110.         case t_null:
  111.             return_error(e_syntaxerror);
  112.         default:
  113.             return code;
  114.            }
  115.         return 0;
  116.     case bt_boolean:
  117.         arg = sgetc(s);
  118.         if ( arg & ~1 )
  119.             return_error(e_syntaxerror);
  120.         make_bool(pref, arg);
  121.         return 0;
  122.     case bt_string_256:
  123.         arg = sgetc(s); goto str;
  124.     case bt_string_64k_msb:
  125.         arg = sgetc(s) << 8;
  126.         if ( seofp(s) )
  127.             return_error(e_syntaxerror);
  128.         arg += sgetc(s);
  129.         goto str;
  130.     case bt_string_64k_lsb:
  131.         arg = sgetc(s);
  132.         if ( seofp(s) )
  133.             return_error(e_syntaxerror);
  134.         arg += sgetc(s) << 8;
  135. str:       {    byte *str = (byte *)alloc(arg, 1, "string token");
  136.         uint rcnt;
  137.         if ( str == 0 )
  138.             return_error(e_VMerror);
  139.         rcnt = sgets(s, str, arg);
  140.         if ( rcnt != arg )
  141.             return_error(e_syntaxerror);
  142.         make_tasv(pref, t_string, a_all, arg, bytes, str);
  143.        }    return 0;
  144.     case bt_litname_system:
  145.         nidx = sgetc(s);
  146.         return array_get(&system_names, nidx, pref);
  147.     case bt_execname_system:
  148.         nidx = sgetc(s);
  149.         code = array_get(&system_names, nidx, pref);
  150.         if ( code < 0 ) return code;
  151.         r_set_attrs(pref, a_executable);
  152.         return 0;
  153.     case bt_litname_user:
  154.         nidx = sgetc(s);
  155.         code = array_get(&user_names, nidx, pref);
  156.         if ( code < 0 ) return code;
  157.         if ( !r_has_type(pref, t_name) )
  158.             return_error(e_undefined);
  159.         return 0;
  160.     case bt_execname_user:
  161.         nidx = sgetc(s);
  162.         code = array_get(&user_names, nidx, pref);
  163.         if ( code < 0 ) return code;
  164.         if ( !r_has_type(pref, t_name) )
  165.             return_error(e_undefined);
  166.         r_set_attrs(pref, a_executable);
  167.         return 0;
  168.     case bt_num_array:
  169.        {    ref *nap;
  170.         uint i;
  171.         ushort asize;
  172.         num_format = sgetc(s);
  173.         if ( !num_is_valid(num_format) )
  174.             return_error(e_syntaxerror);
  175.         s->num_format = num_format;
  176.         code = sgetshort(s, (short *)&ashort);
  177.         if ( code < 0 ) return code;
  178.         asize = ashort;        /* avoid width problems */
  179.         code = alloc_array(pref, a_all, asize, "number array token");
  180.         if ( code < 0 ) return code;
  181.         nap = pref->value.refs;
  182.         for ( i = 0; i < asize; i++ )
  183.            {    ref *np = nap + i;
  184.             int code = sget_encoded_number(s, np);
  185.             switch ( code )
  186.                {
  187.             case t_integer: case t_real:
  188.                 r_set_type(np, code);
  189.                 break;
  190.             case t_null:
  191.                 return_error(e_syntaxerror);
  192.             default:
  193.                 return code;
  194.                }
  195.            }
  196.        }    return 0;
  197.        }
  198.     return_error(e_syntaxerror);
  199. }
  200.  
  201. /* Scan a binary object sequence. */
  202. private int
  203. scan_binary_sequence(register stream *s, ref *pref)
  204. {    ushort top_size = sgetc(s);
  205.     ushort size;
  206.     uint max_array_index;
  207.     uint min_string_index;
  208.     uint index;
  209.     int code;
  210.     register os_ptr op = osp;
  211.     uint global = !alloc_current_local();
  212. #if arch_is_big_endian
  213. #  define must_swap_bytes s_is_lsb(s)
  214. #else
  215. #  define must_swap_bytes s_is_msb(s)
  216. #endif
  217.     if ( top_size == 0 )
  218.        {    /* Extended header (2-byte array size, 4-byte length) */
  219.         ulong lsize;
  220.         if ( (code = sgetshort(s, (short *)&top_size)) < 0 ||
  221.              (code = sgetlong(s, (long *)&lsize)) < 0
  222.            )
  223.             return code;
  224.         if ( (size = (ushort)lsize) != lsize )
  225.             return_error(e_limitcheck);
  226.         if ( size < 8 + sizeof(bin_seq_obj) )
  227.             return_error(e_syntaxerror);
  228.         size -= 8;
  229.        }
  230.     else
  231.        {    /* Normal header (1-byte array size, 2-byte length) */
  232.         if ( (code = sgetshort(s, (short *)&size)) < 0 )
  233.             return code;
  234.         if ( size < 4 + sizeof(bin_seq_obj) )
  235.             return_error(e_syntaxerror);
  236.         size -= 4;
  237.        }
  238.     /* First pass: read objects, handle all but composite. */
  239.     max_array_index = top_size;
  240.     min_string_index = size;
  241.     for ( index = 0; index < max_array_index; index++ )
  242.        {    bin_seq_obj ob;
  243.         byte bt;
  244.         if ( sgets(s, (byte *)&ob, sizeof(bin_seq_obj)) !=
  245.             sizeof(bin_seq_obj)
  246.            )
  247.             return_error(e_syntaxerror);
  248.         if ( ++op > ostop )
  249.             return_error(e_limitcheck);
  250. #define swap_size()\
  251.   if ( must_swap_bytes )\
  252.     bt = ob.size.b[0], ob.size.b[0] = ob.size.b[1], ob.size.b[1] = bt
  253. #define swap_value()\
  254.   if ( must_swap_bytes )\
  255.     bt = ob.value.b[0], ob.value.b[0] = ob.value.b[3], ob.value.b[3] = bt,\
  256.     bt = ob.value.b[1], ob.value.b[1] = ob.value.b[2], ob.value.b[2] = bt
  257.         switch ( ob.tx & 0x7f )
  258.            {
  259.         case bs_null:
  260.             make_null(op); break;
  261.         case bs_integer:
  262.             swap_value();
  263.             make_int(op, ob.value.w);
  264.             break;
  265.         case bs_real:
  266.             if ( ob.size.w != 0 )    /* fixed-point number */
  267.                {    swap_size(); swap_value();
  268.                 ob.value.f = (float)ldexp((float)ob.value.w,
  269.                               -ob.size.w);
  270.                }
  271.             else if ( (s->num_format & ~(num_lsb | num_msb)) !=
  272.                   num_float_native
  273.                 )
  274.                {    swap_value();
  275. #if !arch_floats_are_IEEE
  276.                 /* Convert IEEE float to native float. */
  277.                    {    int expt = (ob.value.w >> 23) & 0xff;
  278.                     long mant = ob.value.w & 0x7fffff;
  279.                     if ( expt == 0 && mant == 0 )
  280.                         ob.value.f = 0;
  281.                     else
  282.                        {    mant += 0x800000;
  283.                         ob.value.f = (float)
  284.                           ldexp((float)mant,
  285.                             expt - 127 - 24);
  286.                        }
  287.                     if ( ob.value.w < 0 )
  288.                         ob.value.f = -ob.value.f;
  289.                    }
  290. #endif
  291.                }
  292.             make_real(op, ob.value.f);
  293.             break;
  294.         case bs_boolean:
  295.             swap_value();
  296.             make_bool(op, (ob.value.w == 0 ? 0 : 1));
  297.             break;
  298.         case bs_string:
  299.             swap_size(); swap_value();
  300.             if ( ob.value.w < max_array_index ||
  301.                 ob.value.w + ob.size.w > size
  302.                )
  303.                 return_error(e_syntaxerror);
  304.             if ( ob.value.w < min_string_index )
  305.                 min_string_index = (uint)ob.value.w;
  306.             make_tasv(op, t_string, (ob.tx < 128 ? a_all :
  307.                   a_all + a_executable), ob.size.w,
  308.                   intval, ob.value.w);
  309.             break;
  310.         case bs_name:
  311.         case bs_eval_name:
  312.             swap_size(); swap_value();
  313.             switch ( ob.size.w )
  314.                {
  315.             case 0:
  316.             case 0xffff:
  317.                 break;
  318.             default:
  319.                 if ( ob.value.w < max_array_index ||
  320.                     ob.value.w + ob.size.w > size
  321.                    )
  322.                     return_error(e_syntaxerror);
  323.                 if ( ob.value.w < min_string_index )
  324.                     min_string_index = (uint)ob.value.w;
  325.                }
  326.             make_tasv(op, t_name,
  327.                   ((ob.tx & 0x7f) == bs_eval_name ? 0 :
  328.                    ob.tx & 0x80 ? a_all + a_executable :
  329.                    a_all), ob.size.w, intval, ob.value.w);
  330.             break;
  331.         case bs_array:
  332.             swap_size(); swap_value();
  333.             if ( ob.value.w + ob.size.w > min_string_index ||
  334.                 ob.value.w & (sizeof(bin_seq_obj) - 1)
  335.                )
  336.                 return_error(e_syntaxerror);
  337.             max_array_index =
  338.               max(max_array_index,
  339.                   ob.value.w / sizeof(bin_seq_obj) + ob.size.w);
  340.             make_tasv(op, t_array, (ob.tx < 128 ? a_all :
  341.                   a_all + a_executable), ob.size.w,
  342.                   intval, ob.value.w);
  343.             break;
  344.         case bs_mark:
  345.             make_mark(op); break;
  346.         default:
  347.             return_error(e_syntaxerror);
  348.            }
  349.        }
  350.     /* Allocate objects and strings. */
  351.     min_string_index =
  352.       min(min_string_index, max_array_index * sizeof(bin_seq_obj));
  353.        {    int code = alloc_array(pref, a_all + a_executable,
  354.                        max_array_index,
  355.                        "binary object sequence(objects)");
  356.         ref *rbase = pref->value.refs;
  357.         uint str_size = size - min_string_index;
  358.         byte *sbase = (byte *)alloc(str_size, 1,
  359.                         "binary object sequence(strings)");
  360.         os_ptr op_top = op;
  361.         if ( code < 0 || sbase == 0 )
  362.            {    /* SHOULD FREE THEM */
  363.             return_error(e_VMerror);
  364.            }
  365.         if ( sgets(s, sbase, str_size) != str_size )
  366.             return_error(e_syntaxerror);
  367.         /* Fix up composites. */
  368.         for ( op = osp; op != op_top; )
  369.           switch ( r_type(++op) )
  370.            {
  371.         case t_string:
  372.             op->value.bytes =
  373.                 sbase + (op->value.intval - min_string_index);
  374.             break;
  375.         case t_array:
  376.             op->value.refs =
  377.                 rbase + (op->value.intval / sizeof(bin_seq_obj));
  378.             break;
  379.         case t_name:
  380.            {    ref rname;
  381.             switch ( r_size(op) )
  382.                {
  383.             case 0:
  384.                 code = array_get(&user_names,
  385.                          op->value.intval, &rname);
  386.                 goto usn;
  387.             case 0xffff:
  388.                 code = array_get(&system_names,
  389.                          op->value.intval, &rname);
  390. usn:                if ( code >= 0 && !r_has_type(&rname, t_name) )
  391.                     return_error(e_undefined);
  392.                 break;
  393.             default:    /* ordinary name */
  394.                 code = name_ref(sbase + (op->value.intval - min_string_index), r_size(op), &rname, 0);
  395.                }
  396.             if ( code < 0 ) return code;
  397.             if ( !r_has_attr(op, a_read) )    /* bs_eval_name */
  398.                {    ref *defp = dict_find_name(&rname);
  399.                 if ( defp == 0 )
  400.                     return_error(e_undefined);
  401.                 if ( !r_is_global(defp) && global )
  402.                     return_error(e_invalidaccess);
  403.                 rname = *defp;
  404.                }
  405.             else if ( r_has_attr(op, a_executable) )
  406.                 r_set_attrs(&rname, a_executable);
  407.             ref_assign(op, &rname);
  408.            }
  409.             break;
  410.            }
  411.         refcpy_to_new(rbase, osp + 1, max_array_index);
  412.         r_set_size(pref, top_size);
  413.        }
  414.     return scan_BOS;
  415. }
  416.